package net.thxjj.adminServer.controller;

import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IMenuService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import net.thxjj.sitermodel.Menu;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/menuManage")
public class MenuController extends BaseController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private IMenuService menuService;


    /**
     * 获取菜单列表
     * @return
     */
    @RequestMapping(value="/menuList",method= RequestMethod.GET)
    @ResponseBody
    public Object menuList(){

        List<Menu> oneMenuList = menuService.getAllMenuList();
        return ResponseModel.getModel(ResultEnum.SUCCESS, oneMenuList);

    }

    /**
     * 增加菜单（包括子菜单或者父菜单）
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody JSONObject jsonObject){

        String menu_NAME = jsonObject.getString("menu_NAME");

        long menu_type = jsonObject.getLongValue("menu_TYPE");

        long menu_ORDER = jsonObject.getLongValue("menu_ORDER");

        String menu_URL = jsonObject.getString("menu_URL");

        String menu_ICON = jsonObject.getString("menu_ICON");

        long parent_id = jsonObject.getLongValue("parent_id");


        if(Tools.isEmpty(menu_NAME)||Tools.isEmpty(menu_URL)||Tools.isEmpty(menu_ICON)||
                !jsonObject.containsKey("menu_TYPE")|| !jsonObject.containsKey("menu_ORDER")|| !jsonObject.containsKey("parent_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("menu_name",menu_NAME);
        parameterMap.put("menu_url",menu_URL);
        parameterMap.put("menu_icon",menu_ICON);
        parameterMap.put("menu_type",menu_type);
        parameterMap.put("sort_num",menu_ORDER);
        parameterMap.put("parent_id",parent_id);

        return menuService.addMenu(parameterMap,this.getSession());
    }

    /**
     * 编辑菜单
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Object edit(@RequestBody JSONObject jsonObject){

        long menu_id = jsonObject.getLongValue("menu_ID");

        String menu_NAME = jsonObject.getString("menu_NAME");

        long menu_type = jsonObject.getLongValue("menu_TYPE");

        long menu_ORDER = jsonObject.getLongValue("menu_ORDER");

        String menu_URL = jsonObject.getString("menu_URL");

        String menu_ICON = jsonObject.getString("menu_ICON");


        if(Tools.isEmpty(menu_NAME)||Tools.isEmpty(menu_URL)||Tools.isEmpty(menu_ICON)||
                !jsonObject.containsKey("menu_ID") || !jsonObject.containsKey("menu_TYPE")|| !jsonObject.containsKey("menu_ORDER")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("menu_name",menu_NAME);
        parameterMap.put("menu_url",menu_URL);
        parameterMap.put("menu_icon",menu_ICON);
        parameterMap.put("menu_type",menu_type);
        parameterMap.put("sort_num",menu_ORDER);
        parameterMap.put("menu_id",menu_id);

        return menuService.editMenu(parameterMap);
    }

    /**
     * 删除菜单
     * @return
     */
    @RequestMapping(value="/del",method=RequestMethod.POST)
    @ResponseBody
    public Object del(@RequestBody JSONObject jsonObject) {

        if(!jsonObject.containsKey("menu_ID")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        long menuid = jsonObject.getLongValue("menu_ID");

        return menuService.delMenu(String.valueOf(menuid));
    }

}
